package com.link.base.base.user.service;

import com.alibaba.fastjson.JSONObject;
import com.link.base.base.user.model.OnlineUser;
import com.link.core.cllog.LCLogger;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author unknown
 * @date unknown
 * @comments 在线用户
 */
@Service
public class OnlineRedisUserServiceImpl implements OnlineRedisUserService {

    /** 查询redis中在线用户 */
    @Override
    public List<OnlineUser> queryOnline() throws Exception {
        List<OnlineUser> list = new ArrayList<OnlineUser>();
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            //获取CRM用户
            Set<String> onlineUserList = jedis.keys("spring:session:crm:sessions:*");
            for (String jsonUserKey : onlineUserList) {
                if (!"spring:session:crm:sessions:expires:".equals(jsonUserKey.substring(0, 36))) {
                    String userJson = jedis.hget(jsonUserKey, "sessionAttr:online");
                    if (!"".equals(userJson) && userJson != null) {
                        String[] ne = userJson.split("\\{");
                        int n = ne[0].length();
                        String fUserJson = userJson.substring(n, userJson.length());
                        OnlineUser user = JSONObject.parseObject(fUserJson, OnlineUser.class);
                        list.add(user);
                    }
                }
            }

            //获取APP用户
            Set<String> onlineAppUserList = jedis.keys("spring:session:app:sessions:*");
            for (String jsonUserKey : onlineAppUserList) {
                if (!"spring:session:app:sessions:expires:".equals(jsonUserKey.substring(0, 36))) {
                    String userJson = jedis.hget(jsonUserKey, "sessionAttr:online");
                    if (!"".equals(userJson) && userJson != null) {
                        String[] ne = userJson.split("\\{");
                        int n = ne[0].length();
                        String fUserJson = userJson.substring(n, userJson.length());
                        OnlineUser user = JSONObject.parseObject(fUserJson, OnlineUser.class);
                        list.add(user);
                    }
                }
            }
            return list;
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /** 删除指定用户的session */
    @Override
    public void deleteSessionByLogin(String login) throws Exception {
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            Set<String> onlineUserList = jedis.keys("spring:session:crm:sessions:*");
            for (String jsonUserKey : onlineUserList) {
                if (!"spring:session:crm:sessions:expires:".equals(jsonUserKey.substring(0, 36))) {
                    String userJson = jedis.hget(jsonUserKey, "sessionAttr:online");
                    if (!"".equals(userJson) && userJson != null) {
                        System.out.println(userJson);
                        String[] ne = userJson.split("\\{");
                        int n = ne[0].length();
                        String fUserJson = userJson.substring(n, userJson.length());
                        OnlineUser user = JSONObject.parseObject(fUserJson, OnlineUser.class);
                        if (login.equals(user.getUsername())) {
                            jedis.del(jsonUserKey);
                            return;
                        }
                    }
                }
            }
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * @author huanglongjian
     * @Description 删除指定session的用户
     */
    @Override
    public void deleteCRMSessionById(String sessionId) throws Exception {
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            jedis.del("spring:session:crm:sessions:" + sessionId);
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * @author huanglongjian
     * @Description 删除指定session的用户
     */
    @Override
    public void deleteAPPSessionById(String sessionId) throws Exception {
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            jedis.del("spring:session:app:sessions:" + sessionId);
        } catch (Exception e) {
            LCLogger.withException(e);
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }
}
